package com.fyzk.activity.lottery.zc.tz_dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

import http.DataUtil.text.TextUtil;

public class JZBF_Dialog_bf extends ZC_Dialog_BaseActivity {

    // 中间
    private View view;

    private TextView tv_sheng, tv_ping, tv_fu;
    private boolean is_sheng = true, is_ping, is_fu;

    private GridView gr;
    private ZCBF_DLG_GR_adapter adapter;
    private List<ZCBF_infor> list_sheng = new ArrayList<ZCBF_infor>();
    private List<ZCBF_infor> list_ping = new ArrayList<ZCBF_infor>();
    private List<ZCBF_infor> list_fu = new ArrayList<ZCBF_infor>();


    private String[] keyArray = null;

    public JZBF_Dialog_bf(Context context, JCInfo infos) {
        super(context, infos);
        init();
    }


    @Override
    protected void init() {
        addBiFen();
        info();
        keyArray = new String[]{infor.BF_10, infor.BF_20,
                infor.BF_21, infor.BF_30, infor.BF_31,
                infor.BF_32, infor.BF_40, infor.BF_41,
                infor.BF_42, infor.BF_50, infor.BF_51,
                infor.BF_52, infor.BF_90, infor.BF_00,
                infor.BF_11, infor.BF_22, infor.BF_33,
                infor.BF_99, infor.BF_01, infor.BF_02,
                infor.BF_12, infor.BF_03, infor.BF_13,
                infor.BF_23, infor.BF_04, infor.BF_14,
                infor.BF_24, infor.BF_05, infor.BF_15,
                infor.BF_25, infor.BF_09,};
    }


    @Override
    protected JCInfo getResult() {
        return infor;
    }

    private void info() {

        view = LayoutInflater.from(context).inflate(R.layout.zc_dialog_bf, null);

        // 中间
        tv_sheng = (TextView) view.findViewById(R.id.zcbf_dialog_tv_sheng);
        tv_ping = (TextView) view.findViewById(R.id.zcbf_dialog_tv_ping);
        tv_fu = (TextView) view.findViewById(R.id.zcbf_dialog_tv_fu);
        gr = (GridView) view.findViewById(R.id.zcbf_dlg_gr);
        adapter = new ZCBF_DLG_GR_adapter(list_sheng);

        center_ll.addView(view);
        // 事件

        tv_sheng.setOnClickListener(new ZCBF_DLG_onClick());
        tv_ping.setOnClickListener(new ZCBF_DLG_onClick());
        tv_fu.setOnClickListener(new ZCBF_DLG_onClick());
        gr.setAdapter(adapter);

    }

    /**
     * 胜、平、负选项卡
     */
    private void xuanXiangKa() {
        if (is_sheng) {
            tv_sheng.setBackgroundResource(R.drawable.touming);
            tv_ping.setBackgroundResource(R.drawable.zcbf_gr_n);
            tv_fu.setBackgroundResource(R.drawable.zcbf_gr_n);
            tv_sheng.setTextColor(Color.WHITE);
            tv_ping.setTextColor(Color.BLACK);
            tv_fu.setTextColor(Color.BLACK);

            adapter.setList(list_sheng);
        } else if (is_ping) {
            tv_sheng.setBackgroundResource(R.drawable.zcbf_gr_n);
            tv_ping.setBackgroundResource(R.drawable.touming);
            tv_fu.setBackgroundResource(R.drawable.zcbf_gr_n);
            tv_sheng.setTextColor(Color.BLACK);
            tv_ping.setTextColor(Color.WHITE);
            tv_fu.setTextColor(Color.BLACK);

            adapter.setList(list_ping);
        } else {
            tv_sheng.setBackgroundResource(R.drawable.zcbf_gr_n);
            tv_fu.setBackgroundResource(R.drawable.touming);
            tv_ping.setBackgroundResource(R.drawable.zcbf_gr_n);
            tv_sheng.setTextColor(Color.BLACK);
            tv_fu.setTextColor(Color.WHITE);
            tv_ping.setTextColor(Color.BLACK);

            adapter.setList(list_fu);
        }
    }

    /**
     * 添加比分集合
     */
    private void addBiFen() {
        list_sheng.clear();
        list_ping.clear();
        list_fu.clear();

        for (int i = 0; i < infor.getTouZhuInfor().size(); i++) {
            if (i >= 6 && i <= 18) {// 胜
                list_sheng.add(infor.getTouZhuInfor().get(i));
            } else if (i >= 19 && i <= 23) {// 平
                list_ping.add(infor.getTouZhuInfor().get(i));
            } else if (i >= 24 && i <= 36) {// 负
                list_fu.add(infor.getTouZhuInfor().get(i));
            }
        }

        list_sheng.add(new ZCBF_infor("全选", "", false));

        list_ping.add(new ZCBF_infor("全选", "", false));

        list_fu.add(new ZCBF_infor("全选", "", false));
    }

    @Override
    public void sureClick() {
        super.sureClick();

        ArrayList<ZCBF_infor> bf_all = new ArrayList<ZCBF_infor>();
        bf_all.addAll(list_sheng);
        bf_all.remove(bf_all.size() - 1);
        bf_all.addAll(list_ping);
        bf_all.remove(bf_all.size() - 1);
        bf_all.addAll(list_fu);
        bf_all.remove(bf_all.size() - 1);
        int clickNum = 0;
        StringBuffer sb = new StringBuffer();
        for (ZCBF_infor info : bf_all) {
            int position = bf_all.indexOf(info);
            if (info.isSelect()) {
                clickNum++;
                sb.append(info.getBiFen());
                sb.append(",");
            }
            infor.btnHGPojo.get(keyArray[position]).isSel = info.isSelect();
        }
        infor.setOnclikNum(clickNum);
        if(!TextUtil.isEmpty(sb.toString().trim())){
            infor.setResult(sb.substring(0, sb.length() - 1));
        }

    }

    /**
     * 单击事件
     *
     * @author Administrator
     */
    class ZCBF_DLG_onClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zcbf_dialog_tv_sheng:
                    is_sheng = true;
                    is_ping = false;
                    is_fu = false;

                    xuanXiangKa();
                    break;
                case R.id.zcbf_dialog_tv_ping:
                    is_sheng = false;
                    is_ping = true;
                    is_fu = false;

                    xuanXiangKa();
                    break;
                case R.id.zcbf_dialog_tv_fu:
                    is_sheng = false;
                    is_ping = false;
                    is_fu = true;

                    xuanXiangKa();
                    break;
                default:
                    break;
            }

        }

    }

    /**
     * 适配器
     *
     * @author Administrator
     */
    class ZCBF_DLG_GR_adapter extends BaseAdapter {

        public List<ZCBF_infor> list = new ArrayList<ZCBF_infor>();
        private int count = 0;

        public ZCBF_DLG_GR_adapter(List<ZCBF_infor> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            if (position == 0) {
                count = 0;
            }
            view = LayoutInflater.from(context).inflate(
                    R.layout.zc_dialog_bf_gr, null);
            final ViewHolder holder = new ViewHolder();

            holder.ll1 = (LinearLayout) view.findViewById(R.id.zcbf_dlg_gr_ll1);
            holder.tv1 = (TextView) view.findViewById(R.id.zcbf_dlg_gr_tv1);
            holder.tv2 = (TextView) view.findViewById(R.id.zcbf_dlg_gr_tv2);

            view.setTag(holder);

            holder.tv1.setText(list.get(position).getBiFen());
            holder.tv2.setText(list.get(position).getPeilv());

            if (!list.get(position).isSelect()) {
                holder.tv1.setTextColor(Color.BLACK);
                holder.tv2.setTextColor(Color.BLACK);
                holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_n);
            } else {
                holder.tv1.setTextColor(Color.RED);
                holder.tv2.setTextColor(Color.RED);
                holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_c);
                if (position != list.size() - 1) {

                    count++;
                }
            }

            holder.ll1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (position == list.size() - 1) {
                        if (list.get(position).isSelect()) {
                            for (ZCBF_infor info : list) {
                                info.setSelect(false);
                            }
                        } else {
                            for (ZCBF_infor info : list) {
                                info.setSelect(true);
                            }

                        }
                        ZCBF_DLG_GR_adapter.this.notifyDataSetChanged();
                        return;
                    }

                    boolean isSel = !list.get(position).isSelect();
                    list.get(position).setSelect(isSel);
                    if (!list.get(position).isSelect()) {
                        holder.tv1.setTextColor(Color.BLACK);
                        holder.tv2.setTextColor(Color.BLACK);
                        holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_n);
                        count--;
                    } else {
                        holder.tv1.setTextColor(Color.RED);
                        holder.tv2.setTextColor(Color.RED);
                        holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_c);
                        count++;
                    }
                    if (count == list.size() - 1) {
                        list.get(list.size() - 1).setSelect(true);
                        ZCBF_DLG_GR_adapter.this.notifyDataSetChanged();
                    } else {
                        list.get(list.size() - 1).setSelect(false);
                        ZCBF_DLG_GR_adapter.this.notifyDataSetChanged();
                    }
                }
            });

            return view;
        }

        public List<ZCBF_infor> getList() {
            return list;
        }

        public void setList(List<ZCBF_infor> list) {
            this.list = list;
            this.notifyDataSetChanged();
        }

        @Override
        public void notifyDataSetChanged() {
            count = 0;
            super.notifyDataSetChanged();
        }

        class ViewHolder {
            LinearLayout ll1;
            TextView tv1;
            TextView tv2;
        }

    }

}
